<template>
	<view>
		<!-- 功能模块选项卡 -->
		<view class="tabs-container">
			<view class="tab-item" :class="{ 'active': currentTab === 'products' }" @click="switchTab('products')">
				<text class="tab-text">商品管理</text>
			</view>
			<view class="tab-item" :class="{ 'active': currentTab === 'orders' }" @click="switchTab('orders')">
				<text class="tab-text">订单管理</text>
			</view>
			<view class="tab-item" :class="{ 'active': currentTab === 'aftersales' }" @click="switchTab('aftersales')">
				<text class="tab-text">售后管理</text>
			</view>
			<view class="tab-item" :class="{ 'active': currentTab === 'dashboard' }" @click="switchTab('dashboard')">
				<text class="tab-text">数据看板</text>
			</view>
		</view>

		<!-- 内容区域 -->
		<view class="content-area">
			<!-- 商品管理 -->
			<view class="tab-content" v-if="currentTab === 'products'">
				<view class="search-bar">
					<input type="text" placeholder="搜索商品名称模糊搜索" v-model="search_text" class="search-input" />
					<button class="search-btn" @click="searchProducts">搜索</button>
				</view>
				<view class="filter-bar">
					<view class="filter-item" :class="{ 'active': goods_state === 'all' }" @click="filterGoods('all')">
						全部</view>
					<view class="filter-item" :class="{ 'active': goods_state === 'sale' }"
						@click="filterGoods('sale')">销售中</view>
					<view class="filter-item" :class="{ 'active': goods_state === 'offsale' }"
						@click="filterGoods('offsale')">已下架</view>
				</view>
				<view class="product-list">
					<view class="product-item" v-for="product in products" :key="product.goods_id">
						<image :src="$util.img(product.goods_image)" mode="aspectFill" class="product-image"></image>
						<view class="product-info">
							<text class="product-name">{{ product.goods_name }}</text>
							<text class="product-price">¥{{ product.price }}</text>
							<text class="product-stock">库存: {{ product.goods_stock }}</text>
							<view class="product-actions">
								<view @click="modifyStock(product)"
									style="background-color: #007aff;color: white;padding: 0 10px;border-radius: 5px;margin-right: 15px;">
									修改库存
								</view>
								<view class="">
									<view v-if="product.goods_state === 1" @click="offGoods(product)"
										style="background-color: red;color: white;padding: 0 10px;border-radius: 5px;">
										下架商品
									</view>
									<view v-else @click="onGoods(product)"
										style="background-color: green;color: white;padding: 0 10px;border-radius: 5px;">
										上架商品
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>

				<view class="pagination" v-if="products.length > 0">
					<button class="page-btn" :disabled="currentPage === 1" @click="prevPage">上一页</button>
					<text class="page-info">{{ currentPage }}/{{ totalPages }}</text>
					<button class="page-btn" :disabled="currentPage === totalPages" @click="nextPage">下一页</button>
				</view>

				<view class="empty-state" v-else>
					<text>暂无商品数据</text>
				</view>
			</view>

			<!-- 订单管理 -->
			<view class="tab-content" v-if="currentTab === 'orders'">
				<view class="search-bar">
					<input type="text" placeholder="搜索订单号或客户名称" v-model="orderSearch" class="search-input" />
					<button class="search-btn" @click="searchOrders">搜索</button>
				</view>

				<view class="filter-bar">
					<view class="filter-item" :class="{ 'active': orderStatus === 'all' }" @click="filterOrders('all')">
						全部</view>
					<view class="filter-item" :class="{ 'active': orderStatus === 'pending' }"
						@click="filterOrders('pending')">待配送</view>
					<view class="filter-item" :class="{ 'active': orderStatus === 'processing' }"
						@click="filterOrders('processing')">配送中</view>
					<view class="filter-item" :class="{ 'active': orderStatus === 'completed' }"
						@click="filterOrders('completed')">已完成</view>
				</view>

				<view class="order-list">
					<view class="order-item" v-for="order in orders" :key="order.order_id">
						<view class="order-header">
							<text class="order-number">订单号: {{ order.order_no }}</text>
							<text :class="'order-status ' + order.status">
								{{ order.order_status_name }}
							</text>
						</view>
						<view class="order-info">
							<text class="customer-name">客户: {{ order.name }}</text>
							<text class="order-date">日期: {{ order.create_time }}</text>
							<text class="order-amount">金额: ¥{{ order.order_money }}</text>
						</view>
						<view class="order-actions">
							<button class="detail-btn" @click="orderDetail(order.order_id)">查看详情</button>
							<button class="process-btn" v-if="order.order_status == 1"
								@click="planDelivery(order.order_id)">派单</button>
							<!-- <button class="change-btn"  v-if="order.order_status == 1" @click="changeDelivery(order.id)">更换配送员</button> -->
						</view>
					</view>
				</view>

				<view class="pagination" v-if="orders.length > 0">
					<button class="page-btn" :disabled="currentOrderPage === 1" @click="prevOrderPage">上一页</button>
					<text class="page-info">{{ currentOrderPage }}/{{ totalOrderPages }}</text>
					<button class="page-btn" :disabled="currentOrderPage === totalOrderPages"
						@click="nextOrderPage">下一页</button>
				</view>

				<view class="empty-state" v-else>
					<text>暂无订单数据</text>
				</view>
			</view>

			<!-- 售后管理 -->
			<view class="tab-content" v-if="currentTab === 'aftersales'">
				<view class="search-bar">
					<input type="text" placeholder="搜索商品名称" v-model="aftersaleSearch" class="search-input" />
					<button class="search-btn" @click="searchAftersales">搜索</button>
				</view>

				<view class="filter-bar">
					<view class="filter-item" :class="{ 'active': aftersaleStatus === 'all' }"
						@click="filterAftersales('all')">全部</view>
					<view class="filter-item" :class="{ 'active': aftersaleStatus === 'pending' }"
						@click="filterAftersales('pending')">待处理</view>
					<view class="filter-item" :class="{ 'active': aftersaleStatus === 'processing' }"
						@click="filterAftersales('processing')">处理中</view>
					<view class="filter-item" :class="{ 'active': aftersaleStatus === 'completed' }"
						@click="filterAftersales('completed')">已完成</view>
				</view>

				<view class="aftersale-list">
					<view class="aftersale-item" v-for="aftersale in aftersalesList" :key="aftersale.order_goods_id">
						<view class="aftersale-header">
							<text class="aftersale-number">售后单号: {{ aftersale.refund_no }}</text>
							<text :class="'aftersale-status ' + aftersale.status">
								{{ aftersale.refund_status_name }}
							</text>
						</view>
						<view class="aftersale-info">
							<text class="customer-name">商品名称: {{ aftersale.goods_name }}</text>
							<text class="order-number">原订单: {{ aftersale.order_no }}</text>
							<text class="aftersale-type">类型:{{ aftersale.refund_type_name }}</text>
							<text class="aftersale-reason" v-if="aftersale.refund_reason">原因: {{ aftersale.refund_reason
							}}</text>
						</view>



						<view class="product-actions">
							<view @click="viewAftersaleDetail(aftersale.order_goods_id)"
								style="background-color: #007aff;color: white;padding: 0 10px;border-radius: 5px;margin-right: 15px;">
								查看详情
							</view>

							<view v-if="aftersale.refund_status == 1"
								@click="processAftersale(aftersale.order_goods_id)"
								style="background-color: red;color: white;padding: 0 10px;border-radius: 5px;">
								处理
							</view>
							<view v-if="aftersale.refund_status == 6 || aftersale.refund_status == 2"
								@click="zhuanzhangAftersale(aftersale.order_goods_id)"
								style="background-color: green;color: white;padding: 0 10px;border-radius: 5px;">
								转账
							</view>


							<view v-if="aftersale.refund_status != 3" @click="closeAftersale(aftersale.order_goods_id)"
								style="background-color: red;color: white;padding: 0 10px;border-radius: 5px;margin-left: 15px;">
								关闭维权
							</view>
						</view>



					</view>
				</view>

				<view class="pagination" v-if="aftersalesList.length > 0">
					<button class="page-btn" :disabled="currentAftersalePage === 1"
						@click="prevAftersalePage">上一页</button>
					<text class="page-info">{{ currentAftersalePage }}/{{ totalAftersalePages }}</text>
					<button class="page-btn" :disabled="currentAftersalePage === totalAftersalePages"
						@click="nextAftersalePage">下一页</button>
				</view>

				<view class="empty-state" v-else>
					<text>暂无售后数据</text>
				</view>
			</view>

			<!-- 数据看板 -->
			<view class="tab-content" v-if="currentTab === 'dashboard'">
				<!-- 交易概况 -->
				<view class="dashboard-section">
					<view class="section-header">
						<text class="section-title">交易概况</text>
						<picker mode="selector" :range="dateRanges" :value="dateRangeIndex" @change="changeDateRange"
							class="date-selector">
							<view class="date-selector-text">
								{{ dateRanges[dateRangeIndex] }}
								<uni-icons type="down" size="12" color="#999" class="date-icon"></uni-icons>
							</view>
						</picker>
					</view>

					<view class="overview-cards">
						<view class="overview-card">
							<text class="card-label">实际收入(元)</text>
							<text class="card-value">{{ dashboardData.estimatedIncome }}</text>
						</view>
						<view class="overview-card">
							<text class="card-label">订单收入(元)</text>
							<text class="card-value">{{ dashboardData.operatingIncome }}</text>
						</view>
						<view class="overview-card">
							<text class="card-label">退款(元)</text>
							<text class="card-value">{{ dashboardData.operatingExpense }}</text>
						</view>
					</view>
					<!-- <view class="overview-cards">
						<view class="overview-card">
							<text class="card-label">订单数量</text>
							<text class="card-value">{{ dashboardData.estimatedIncome }}</text>
						</view>
						<view class="overview-card">
							<text class="card-label">平均单额(元)</text>
							<text class="card-value">{{ dashboardData.operatingIncome }}</text>
						</view>
					</view> -->
				</view>
				<!-- 每类商品信息 -->
				<view v-for="(item,index) in dashboardData.orderinfo">
					<view style="font-weight: bold;margin-top: 20rpx;">{{item.name}}</view>
					<view class="box">
						<view class="flex">
							<view class="tabletd" style="width: 330rpx;">分类</view>
							<view class="tabletd">总金额</view>
							<view class="tabletd">总笔数</view>
							<view class="tabletd">平均单额</view>
							<view class="tabletd">浏览量</view>
						</view>
						<view class="flex" v-for="(itm,idx) in item.list">
							<view class="tabletd" style="width: 330rpx;">{{itm.category_name}} </view>
							<view class="tabletd">{{itm.order_pay_money}}</view>
							<view class="tabletd">{{itm.order_pay_count}}</view>
							<view class="tabletd">{{itm.avg_order_pay_money}}</view>
							<view class="tabletd">{{itm.goods_view_count}}</view>
						</view>
					</view>
				</view>

				<!-- <view class="dashboard-section">
					<view v-for="(item,index) in dashboardData.hospital_department" :key="index">
						<view class="section-title">{{item.hospital_name}}</view>
						<view class="overview-cards department" v-for="(itm,idex) in item.department_list" :key="idx">
							<view>{{itm.department_name}}</view>
							<view>￥{{itm.order_money}}</view>
						</view>
					</view>
				</view> -->


			</view>
		</view>


		<!-- 修改库存弹窗 -->
		<uni-popup ref="stockPopup">
			<view style="background-color: #fff;width: 300px;padding-bottom: 15px ;">
				<view style="padding: 15px;font-weight: bold;text-align: center;font-size: 16px;">
					修改【{{ currentGoods.goods_name }}】库存</view>
				<view style="padding:0 15px;">
					<view style="padding: 5px 0;">
						<text style="color: #999;">原有库存：</text>
						<text style="">{{ currentGoods.goods_stock }}</text>
					</view>
					<view style="padding: 5px 0;display: flex;align-items: center;">
						<view style="color: #999;">现有库存：</view>
						<view>
							<input type="text" v-model="newStock" placeholder="请输入现有库存数量"
								style="border-bottom: 1px solid #eee;font-weight: bold;" />
						</view>
					</view>
				</view>
				<view style="display: flex;font-weight: bold;padding-top: 15px;">
					<view style="width: 50%;text-align: center;font-size: 16px;" @click="$refs.stockPopup.close()">
						取消
					</view>
					<view style="width: 50%;text-align: center;color: red;border-left: 1px solid #eee;font-size: 16px;"
						@click="editGoodsStock()">
						确认
					</view>
				</view>
			</view>
		</uni-popup>

		<!-- 安排配送员 -->
		<uni-popup ref="planDeliveryPopup">
			<view style="background-color: #fff;width: 300px;padding-bottom: 15px ;">
				<view style="padding: 15px;font-weight: bold;text-align: center;font-size: 16px;">分配配送员</view>
				<view style="padding:0 15px;">
					<view style="padding: 5px 0;display: flex;align-items: center;">
						<view style="color: #999;">配送员：</view>
						<view>
							<picker :range="deliveryList" range-key="nickname" @change="deliveryChange">
								<view style="width:400rpx;">
									{{ deliveryPerson.nickname }}
								</view>
							</picker>
						</view>
					</view>
				</view>
				<view style="display: flex;font-weight: bold;padding-top: 15px;">
					<view style="width: 50%;text-align: center;font-size: 16px;"
						@click="$refs.planDeliveryPopup.close()">
						取消
					</view>
					<view style="width: 50%;text-align: center;color: red;border-left: 1px solid #eee;font-size: 16px;"
						@click="addplanDelivery">
						确认
					</view>
				</view>
			</view>
		</uni-popup>

		<!-- 更换配送员 -->
		<uni-popup ref="changeDeliveryPopup">
			<view style="background-color: #fff;width: 300px;padding-bottom: 15px ;">
				<view style="padding: 15px;font-weight: bold;text-align: center;font-size: 16px;">更换配送员</view>
				<view style="padding:0 15px;">
					<view style="padding: 5px 0;display: flex;align-items: center;">
						<view style="color: #999;">配送员：</view>
						<view>
							<picker :range="deliveryList" @change="deliveryChange">
								<view style="width:400rpx;">
									{{ deliveryPerson ? deliveryPerson : '请选择配送员' }}
								</view>
							</picker>
						</view>
					</view>
				</view>
				<view style="display: flex;font-weight: bold;padding-top: 15px;">
					<view style="width: 50%;text-align: center;font-size: 16px;"
						@click="$refs.changeDeliveryPopup.close()">
						取消
					</view>
					<view style="width: 50%;text-align: center;color: red;border-left: 1px solid #eee;font-size: 16px;">
						确认
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
export default {
	data() {
		return {
			deliveryPerson: { member_id: '', nickname: '请选择配送员' },
			deliveryList: [],
			currentGoods: '',
			newStock: '',
			currentTab: 'products', // 当前选中的选项卡: products, orders, aftersales, dashboard
			// 商品管理数据
			products: [],
			search_text: '',
			currentPage: 1,
			totalPages: 1,
			goods_state: 'all',
			// 订单管理数据
			orders: [],
			orderSearch: '',
			orderStatus: 'all', // all, pending, processing, completed, cancelled
			currentOrderPage: 1,
			totalOrderPages: 1,
			// 售后管理数据
			aftersalesList: [],
			aftersaleSearch: '',
			aftersaleStatus: 'all', // all, pending, processing, completed
			currentAftersalePage: 1,
			totalAftersalePages: 1,
			paidanId: 0,
			// 数据看板数据
			dateRanges: ['今日实时', '昨日', '本周', '本月', '本季度', '本年'],
			dateRangeIndex: 0,
			dashboardData: {
				estimatedIncome: '0.00',
				operatingIncome: '0.00',
				operatingExpense: '0.00',
				hospital_department: [],
				orderinfo:[]
			}
		};
	},
	onLoad(option) {
		this.currentTab = option.currentTab??'products'
		// 加载商品数据
		this.loadProducts();
	},
	onShow() {
		if (this.currentTab === 'products') {
			this.loadProducts();
		} else if (this.currentTab === 'orders') {
			this.loadOrders();
		} else if (this.currentTab === 'aftersales') {
			this.loadAftersales();
		} else if (this.currentTab === 'dashboard') {
			this.loadDashboardData();
		}
	},
	methods: {
		editGoodsStock() {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			uni.showLoading({
				mask: true
			})
			this.$api.sendRequest({
				url: '/api/admin/editGoodsStock',
				data: {
					goods_id: this.currentGoods.goods_id,
					stock: this.newStock
				},
				success: res => {
					this.$util.showToast({
						title: res.message
					})
					setTimeout(() => {
						this.loadProducts()
					}, 1000)
				},
				complete: com => {
					uni.hideLoading()
					this.$refs.stockPopup.close()
				}
			})
		},
		//选择配送员
		deliveryChange(e) {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			this.deliveryPerson = this.deliveryList[e.detail.value]
		},
		//上架商品
		onGoods(product) {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			uni.showModal({
				title: '上架商品',
				content: '确认上架商品【' + product.goods_name + '】吗？',
				success: succ => {
					if (succ.confirm) {
						uni.showLoading({
							mask: true
						})
						this.$api.sendRequest({
							url: '/api/admin/goods_status',
							data: {
								goods_ids: product.goods_id,
								goods_state: 1
							},
							success: res => {
								this.$util.showToast({
									title: res.message
								})
								setTimeout(() => {
									this.loadProducts()
								}, 500)
							},
							complete: com => {
								uni.hideLoading()
							}
						})
					}
				}
			})
		},
		//下架商品
		offGoods(product) {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			uni.showModal({
				title: '下架商品',
				content: '确认下架商品【' + product.goods_name + '】吗？',
				success: succ => {
					if (succ.confirm) {
						uni.showLoading({
							mask: true
						})
						this.$api.sendRequest({
							url: '/api/admin/goods_status',
							data: {
								goods_ids: product.goods_id,
								goods_state: 0
							},
							success: res => {
								this.$util.showToast({
									title: res.message
								})
								setTimeout(() => {
									this.loadProducts()
								}, 500)
							},
							complete: com => {
								uni.hideLoading()
							}
						})
					}
				}
			})
		},
		//修改库存
		modifyStock(product) {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			this.newStock = ''
			this.currentGoods = product
			this.$refs.stockPopup.open()
		},
		// 切换选项卡
		switchTab(tab) {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			console.log(tab);
			this.currentTab = tab;
			if (tab === 'products') {
				this.loadProducts();
			} else if (tab === 'orders') {
				this.loadOrders();
			} else if (tab === 'aftersales') {
				this.loadAftersales();
			} else if (tab === 'dashboard') {
				this.loadDashboardData();
			}
		},

		// 数据看板方法
		// 切换日期范围
		changeDateRange(e) {
			this.dateRangeIndex = e.detail.value;
			this.loadDashboardData();
		},

		// 获取日期范围对应的开始和结束时间
		getDateRangeParams(rangeIndex) {
			const now = new Date();
			let start_time = '';
			let end_time = '';
			console.log('rangeIndex' + rangeIndex);
			switch (rangeIndex) {
				case '0': // 今日实时
					start_time = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime() / 1000;
					end_time = now.getTime() / 1000;
					break;
				case '1': // 昨日
					start_time = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1).getTime() / 1000;
					end_time = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0).getTime() / 1000 - 1;
					break;
				case '2': // 本周
					const day = now.getDay() || 7; // 调整周日为7
					start_time = new Date(now.getFullYear(), now.getMonth(), now.getDate() - day + 1).getTime() / 1000;
					end_time = new Date(now.getFullYear(), now.getMonth(), now.getDate() - day + 7, 23, 59, 59).getTime() / 1000;
					break;
				case '3': // 本月
					start_time = new Date(now.getFullYear(), now.getMonth(), 1).getTime() / 1000;
					end_time = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59).getTime() / 1000;
					break;
				case '4': // 本季度
					const quarter = Math.floor(now.getMonth() / 3);
					start_time = new Date(now.getFullYear(), quarter * 3, 1).getTime() / 1000;
					end_time = new Date(now.getFullYear(), quarter * 3 + 3, 0, 23, 59, 59).getTime() / 1000;
					break;
				case '5': // 本年
					start_time = new Date(now.getFullYear(), 0, 1).getTime() / 1000;
					end_time = new Date(now.getFullYear(), 11, 31, 23, 59, 59).getTime() / 1000;
					break;
				default:
					start_time = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime() / 1000;
					end_time = now.getTime() / 1000;
					break;
			}
			return { start_time, end_time };
		},

		// 加载数据看板数据
		loadDashboardData() {
			const { start_time, end_time } = this.getDateRangeParams(this.dateRangeIndex);
			console.log('API请求参数:', start_time, end_time);
			uni.showLoading({
				mask: true
			})

			this.$api.sendRequest({
				url: '/api/admin/getStatTotal',
				data: {
					start_time: start_time,
					end_time: end_time
				},
				success: res => {
					console.log('API返回数据:', res);
					if (res.code >= 0) {
						this.dashboardData = {
							estimatedIncome: res.data.expected_earnings_total_money || '0.00',
							operatingIncome: res.data.earnings_total_money || '0.00',
							operatingExpense: res.data.expenditure_total_money || '0.00',
							hospital_department: res.data.hospital_department || [],
							orderinfo: res.data.orderinfo || [],
							
						}
					} else {
						uni.showToast({
							title: res.msg || '获取数据失败',
							icon: 'none',
							duration: 2000
						});
					}
				},
				fail: (err) => {
					console.error('API请求失败:', err);
					uni.showToast({
						title: '网络请求失败',
						icon: 'none',
						duration: 2000
					});
				},
				complete: com => {
					uni.hideLoading()
				}
			})
		},

		// 商品管理方法
		loadProducts() {
			let goodsStatus = 0
			if (this.goods_state == 'sale') {
				goodsStatus = 1
			} else if (this.goods_state == 'offsale') {
				goodsStatus = 2
			} else {
				goodsStatus = 0
			}

			uni.showLoading()
			this.$api.sendRequest({
				url: '/api/admin/goodslists',
				data: {
					search_text: this.search_text,
					goods_state: goodsStatus,
					page: this.currentPage,
				},
				success: res => {
					this.products = res.data.list
					this.totalPages = res.data.page_count
				},
				complete() {
					uni.hideLoading()
				}
			})
		},
		searchProducts() {
			// 模拟搜索商品
			this.loadProducts();
		},
		updateStock(id, stock) {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			const product = this.products.find(p => p.id === id);
			if (product) {
				product.stock = stock;
				// 这里可以添加更新库存的API调用
				uni.showToast({
					title: '库存已更新',
					icon: 'success'
				});
			}
		},
		toggleStatus(id, isOn) {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			const product = this.products.find(p => p.id === id);
			if (product) {
				product.status = isOn ? 'on' : 'off';
				// 这里可以添加上下架的API调用
				uni.showToast({
					title: `商品已${isOn ? '上架' : '下架'}`,
					icon: 'success'
				});
			}
		},
		addProduct() {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			// 跳转到添加商品页面
			uni.navigateTo({
				url: '/pages_tool/goods/add'
			});
		},
		editProduct(id) {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			// 跳转到编辑商品页面
			uni.navigateTo({
				url: `/pages_tool/goods/edit?id=${id}`
			});
		},
		prevPage() {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			if (this.currentPage > 1) {
				this.currentPage--;
				this.loadProducts();
			}
		},
		nextPage() {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			if (this.currentPage < this.totalPages) {
				this.currentPage++;
				this.loadProducts();
			}
		},

		// 订单管理方法
		loadOrders() {
			let orderStatus = ''
			if (this.orderStatus == 'all') {
				orderStatus = ''
			} else if (this.orderStatus == 'pending') {
				orderStatus = 1
			} else if (this.orderStatus == 'processing') {
				orderStatus = 3
			} else if (this.orderStatus == 'completed') {
				orderStatus = 4
			}
			uni.showLoading()
			this.$api.sendRequest({
				url: '/api/admin/orderList',
				data: {
					search: this.orderSearch,
					order_status: orderStatus,
					page: this.currentOrderPage,
				},
				success: res => {
					this.orders = res.data.list
					this.totalOrderPages = res.data.page_count
				},
				complete() {
					uni.hideLoading()
				}
			})

			//获取配送员列表
			this.$api.sendRequest({
				url: '/api/admin/deliveryman',
				data: {},
				success: res => {
					this.deliveryList = res.data;
				},
				complete() {
					uni.hideLoading()
				}
			})
		},
		searchOrders() {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			// 模拟搜索订单
			this.loadOrders();
		},
		filterGoods(status) {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			this.goods_state = status;
			this.loadProducts()
		},
		filterOrders(status) {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			this.orderStatus = status;
			this.loadOrders();
		},
		orderDetail(order_id) {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			console.log('订单ID' + order_id);
			// 跳转到订单详情页面
			uni.navigateTo({
				url: '/pages_tool/administrator/detail?order_id=' + order_id
			});
		},
		planDelivery(id) {
			uni.requestSubscribeMessage({
				tmplIds: ['4i3Lbuk4F_WezjPvvJgFK-76QGxupghZqebQtbb5SbA','BEM0aJf3GdAkY28RfIgbfLSLUigZzB5SuKG-J5k5H7k','Y_QJWUhJRiT2orhZCjuUgbtQ8rq0K6zecekSJR35CjY'],
				success(res) {
					console.log(res);
				},
				fail(e) {
					console.log(e);
				}
			})
			console.log(id);
			this.paidanId = id;
			this.deliveryPerson = { member_id: '', nickname: '请选择配送员' };
			this.$refs.planDeliveryPopup.open()
		},
		changeDelivery(id) {
			this.deliveryPerson = { member_id: '', nickname: '请选择配送员' };
			this.$refs.changeDeliveryPopup.open()
		},
		// 确认配送
		addplanDelivery() {
			if (this.deliveryPerson.member_id == '') {
				uni.showToast({
					title: '请选择配送员',
					icon: 'none'
				});
				return;
			}
			this.$api.sendRequest({
				url: '/api/admin/dispatch',
				data: {
					order_id: this.paidanId,
					member_id: this.deliveryPerson.member_id
				},
				success: res => {
					this.$refs.planDeliveryPopup.close()
					this.loadOrders();
				},
				complete() {
					uni.hideLoading()
				}
			})
		},
		prevOrderPage() {
			if (this.currentOrderPage > 1) {
				this.currentOrderPage--;
				this.loadOrders();
			}
		},
		nextOrderPage() {
			if (this.currentOrderPage < this.totalOrderPages) {
				this.currentOrderPage++;
				console.log(this.currentOrderPage);
				this.loadOrders();
			}
		},

		// 售后管理方法
		loadAftersales() {

			// aftersaleSearch: '',
			// aftersaleStatus: 'all', // all, pending, processing, completed
			// currentAftersalePage: 1,
			// totalAftersalePages: 1,
			this.$api.sendRequest({
				url: '/api/admin/afterlists',
				data: {
					sku_name: this.aftersaleSearch,
					refund_status: this.aftersaleStatus,
					page: this.currentAftersalePage,
				},
				success: res => {
					this.aftersalesList = res.data.list
					this.totalAftersalePages = res.data.page_count
				},
				complete() {
					uni.hideLoading()
				}
			})
			// // 模拟加载售后数据
			// this.aftersales = [{
			// 		id: 1,
			// 		aftersaleNo: 'AFS20230501001',
			// 		orderNo: 'ORD20230501001',
			// 		customerName: '张三',
			// 		type: 'refund',
			// 		reason: '商品质量问题',
			// 		status: 'pending'
			// 	},
			// 	{
			// 		id: 2,
			// 		aftersaleNo: 'AFS20230501002',
			// 		orderNo: 'ORD20230501003',
			// 		customerName: '王五',
			// 		type: 'return',
			// 		reason: '买错商品',
			// 		status: 'processing'
			// 	},
			// 	{
			// 		id: 3,
			// 		aftersaleNo: 'AFS20230502001',
			// 		orderNo: 'ORD20230502001',
			// 		customerName: '赵六',
			// 		type: 'exchange',
			// 		reason: '尺寸不合适',
			// 		status: 'completed'
			// 	},
			// 	{
			// 		id: 4,
			// 		aftersaleNo: 'AFS20230503001',
			// 		orderNo: 'ORD20230502002',
			// 		customerName: '钱七',
			// 		type: 'refund',
			// 		reason: '商品缺货',
			// 		status: 'pending'
			// 	}
			// ];
			// this.totalAftersalePages = 1;
		},
		searchAftersales() {
			// 模拟搜索售后
			this.loadAftersales();
		},
		filterAftersales(status) {
			this.aftersaleStatus = status;
			this.loadAftersales();
		},
		viewAftersaleDetail(id) {
			// 跳转到售后详情页面
			uni.navigateTo({
				url: `/pages_tool/administrator/refund/detail?id=${id}`
			});
		},
		processAftersale(id) {
			uni.navigateTo({
				url: '/pages_tool/administrator/refund/agree?id=' + id
			});
		},
		zhuanzhangAftersale(id) {
			uni.navigateTo({
				url: '/pages_tool/administrator/refund/transfer?id=' + id
			});
		},
		prevAftersalePage() {
			if (this.currentAftersalePage > 1) {
				this.currentAftersalePage--;
				this.loadAftersales();
			}
		},
		nextAftersalePage() {
			if (this.currentAftersalePage < this.totalAftersalePages) {
				this.currentAftersalePage++;
				this.loadAftersales();
			}
		},
		closeAftersale(id) {
			uni.showModal({
				title: '提示',
				content: '确定关闭维权吗？',
				success: (res) => {
					if (res.confirm) {
						this.$api.sendRequest({
							url: '/api/admin/close',
							data: {
								order_goods_id: id
							},
							success: res => {
								if (res.code >= 0) {
									uni.showToast({
										title: '操作成功',
										icon: 'success',
										duration: 2000
									});
									this.loadAftersales();
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none',
										duration: 2000
									});
								}
							}
						})
					}
				}
			})
		}
	}
};
</script>

<style scoped>
/* 选项卡样式 */
.tabs-container {
	display: flex;
	justify-content: space-around;
	padding: 10rpx 0;
	border-bottom: 1rpx solid #e0e0e0;
	margin-bottom: 20rpx;
	background-color: #fff;
}

.tab-item {
	display: flex;
	flex-direction: column;
	align-items: center;
	padding: 15rpx;
}

.tab-item.active {
	color: #007aff;
	font-weight: bold;
}

.tab-text {
	font-size: 28rpx;
	margin-top: 5rpx;
}

/* 内容区域样式 */
.content-area {
	border-radius: 10rpx;
	padding: 20rpx;
}

/* 搜索栏样式 */
.search-bar {
	display: flex;
	margin-bottom: 20rpx;
}

.search-input {
	flex: 1;
	height: 70rpx;
	border: 1rpx solid #e0e0e0;
	border-radius: 8rpx;
	padding: 0 20rpx;
	font-size: 28rpx;
	background-color: #fff;
}

.search-btn {
	width: 140rpx;
	height: 70rpx;
	line-height: 70rpx;
	background-color: #007aff;
	color: #fff;
	font-size: 28rpx;
	border-radius: 8rpx;
	margin-left: 15rpx;
}

/* 操作栏样式 */
.action-bar {
	display: flex;
	justify-content: flex-end;
	margin-bottom: 20rpx;
}

.add-btn {
	width: 140rpx;
	height: 70rpx;
	line-height: 70rpx;
	background-color: #07c160;
	color: #fff;
	font-size: 28rpx;
	border-radius: 8rpx;
}

/* 筛选栏样式 */
.filter-bar {
	display: flex;
	overflow-x: auto;
	margin-bottom: 20rpx;
	padding-bottom: 10rpx;
}

.filter-item {
	padding: 0 20rpx;
	background-color: #fff;
	border-radius: 40rpx;
	font-size: 28rpx;
	margin-right: 15rpx;
	white-space: nowrap;
}

.filter-item.active {
	background-color: #007aff;
	color: #fff;
}

/* 商品列表样式 */
.product-list {
	margin-bottom: 20rpx;
}

.product-item {
	display: flex;
	background-color: #fff;
	border-radius: 10rpx;
	padding: 20rpx;
	margin-bottom: 20rpx;
}

.product-image {
	width: 160rpx;
	height: 160rpx;
	border-radius: 8rpx;
}

.product-info {
	flex: 1;
	margin-left: 20rpx;
	display: flex;
	flex-direction: column;
}

.product-name {
	font-size: 32rpx;
	font-weight: bold;
	margin-bottom: 10rpx;
}

.product-price {
	font-size: 30rpx;
	color: #e64340;
	margin-bottom: 10rpx;
}

.product-stock {
	font-size: 26rpx;
	color: #666;
	margin-bottom: 10rpx;
}

.product-actions {
	display: flex;
	align-items: center;
	margin-top: 10rpx;
	justify-content: flex-end;
}

.status-text {
	font-size: 26rpx;
	margin-left: 10rpx;
	margin-right: 20rpx;
}

.edit-btn {
	width: 100rpx;
	height: 50rpx;
	line-height: 50rpx;
	background-color: #007aff;
	color: #fff;
	font-size: 26rpx;
	border-radius: 8rpx;
}

/* 订单列表样式 */
.order-list {
	margin-bottom: 20rpx;
}

.order-item {
	background-color: #fff;
	border-radius: 10rpx;
	padding: 20rpx;
	margin-bottom: 20rpx;
}

.order-header {
	display: flex;
	justify-content: space-between;
	margin-bottom: 15rpx;
}

.order-number {
	font-size: 28rpx;
	font-weight: bold;
}

.order-status {
	font-size: 26rpx;
	padding: 5rpx 15rpx;
	border-radius: 15rpx;
}

.pending {
	background-color: #fff3cd;
	color: #856404;
}

.processing {
	background-color: #d1ecf1;
	color: #0c5460;
}

.completed {
	background-color: #d4edda;
	color: #155724;
}

.cancelled {
	background-color: #f8d7da;
	color: #721c24;
}

.order-info {
	margin-bottom: 15rpx;
}

.customer-name,
.order-date,
.order-amount {
	display: block;
	font-size: 28rpx;
	color: #333;
	margin-bottom: 8rpx;
}

.order-actions {
	display: flex;
	justify-content: flex-end;
}

.detail-btn {
	background-color: #007aff;
	color: #fff;
	font-size: 26rpx;
	padding: 0 20rpx;
	height: 60rpx;
	line-height: 60rpx;
	border-radius: 8rpx;
	margin-right: 15rpx;
}

.process-btn {
	background-color: #07c160;
	color: #fff;
	font-size: 26rpx;
	padding: 0 20rpx;
	height: 60rpx;
	line-height: 60rpx;
	border-radius: 8rpx;
}

.change-btn {
	background-color: #ff5500;
	color: #fff;
	font-size: 26rpx;
	padding: 0 20rpx;
	height: 60rpx;
	line-height: 60rpx;
	border-radius: 8rpx;
}

/* 售后列表样式 */
.aftersale-list {
	margin-bottom: 20rpx;
}

.aftersale-item {
	background-color: #fff;
	border-radius: 10rpx;
	padding: 20rpx;
	margin-bottom: 20rpx;
}

.aftersale-header {
	display: flex;
	justify-content: space-between;
	margin-bottom: 15rpx;
}

.aftersale-number {
	font-size: 28rpx;
	font-weight: bold;
}

.aftersale-status {
	font-size: 26rpx;
	padding: 5rpx 15rpx;
	border-radius: 15rpx;
}

.aftersale-info {
	margin-bottom: 15rpx;
}

.aftersale-type,
.aftersale-reason {
	display: block;
	font-size: 28rpx;
	color: #333;
	margin-bottom: 8rpx;
}

/* 分页样式 */
.pagination {
	display: flex;
	justify-content: center;
	align-items: center;
	margin: 30rpx 0;
}

.page-btn {
	border: 1rpx solid #e0e0e0;
	border-radius: 8rpx;
	text-align: center;
	font-size: 30rpx;
}

.page-info {
	margin: 0 20rpx;
	font-size: 28rpx;
	color: #666;
}

/* 空状态样式 */
.empty-state {
	text-align: center;
	font-size: 12px;
	color: #999;
	padding: 50% 0;
}

/* 数据看板样式 */
.dashboard-section {
	background-color: #fff;
	border-radius: 10rpx;
	padding: 20rpx;
	margin-bottom: 20rpx;
}

.section-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 20rpx;
}

.section-title {
	font-size: 32rpx;
	font-weight: bold;
	color: #333;
}

.date-selector {
	display: flex;
	align-items: center;
}

.date-selector-text {
	display: flex;
	align-items: center;
	font-size: 28rpx;
	color: #666;
}

.date-icon {
	margin-left: 5rpx;
}

.overview-cards {
	display: flex;
	justify-content: space-between;
}

.overview-card {
	flex: 1;
	text-align: center;
	padding: 15rpx;
}

.card-label {
	display: block;
	font-size: 26rpx;
	color: #666;
	margin-bottom: 10rpx;
}

.card-value {
	display: block;
	font-size: 36rpx;
	font-weight: bold;
	color: #333;
}

.section-total {
	font-size: 32rpx;
	font-weight: bold;
	color: #ff6b35;
}

.data-grid {
	display: flex;
	flex-wrap: wrap;
}

.data-item {
	width: 33.33%;
	padding: 15rpx 0;
}

.item-label {
	display: block;
	font-size: 26rpx;
	color: #666;
	margin-bottom: 10rpx;
}

.item-value {
	display: block;
	font-size: 30rpx;
	color: #333;
}
.department{
	padding: 10rpx 0 10rpx 20rpx;
	border-bottom: 1px solid #eeeeee;
}
.flex{
	display: flex;
	justify-content: space-between;
}
.tabletd{
	width: 20%;
	border-left: 1rpx solid #dedede;
	border-top: 1rpx solid #dedede;
	text-align: center;
	font-size: 26rpx;
}
.box{
	border-bottom: 1rpx solid #dedede;
	border-right: 1rpx solid #dedede;
}
</style>